package com.atguigu.edu.publisher.controller;

import com.atguigu.edu.publisher.beans.TradeFullOrderAmount;
import com.atguigu.edu.publisher.beans.TradeProvinceOrderAmount;
import com.atguigu.edu.publisher.service.TradeStatsService;
import com.atguigu.edu.publisher.utils.DateFormatUtil;
import org.apache.flink.api.java.tuple.Tuple3;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@RestController
public class TradeStatsController {
    @Autowired
    private TradeStatsService tradeStatesService;

    @RequestMapping("/province")
    public String getProvinceAmount(@RequestParam(value = "date", defaultValue = "0") Integer date) {

        if (date == 0) {
            date = DateFormatUtil.now();
        }
        //调用service 层获取各省份交易额
        List<TradeProvinceOrderAmount> provinceAmountList = tradeStatesService.getProvinceAmount(date);
        StringBuilder jsonB = new StringBuilder("{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": {\n" +
                "    \"mapData\": [\n"
        );

        for (int i = 0; i < provinceAmountList.size(); i++) {
            TradeProvinceOrderAmount tradeProvinceOrderAmount = provinceAmountList.get(i);
            jsonB.append("{" + "\"name\": \"" + tradeProvinceOrderAmount.getProvinceName() + "\",\"value\":" + tradeProvinceOrderAmount.getUserCount() + "," + "\"tooltipValues\": [" +
                    +tradeProvinceOrderAmount.getOrderCount() + "," + tradeProvinceOrderAmount.getOrderAmount() + "]" + "}");
            if (i < provinceAmountList.size() - 1) {
                jsonB.append(",");
            }
        }
        jsonB.append("],\n" +
                "    \"valueName\": \"下单总次数\",\n" +
                "    \"tooltipNames\": [\n" +
                "      \"下单总人数\",\n" +
                "      \"下单总金额\"\n" +
                "    ]\n" +
                "  }\n" +
                "}\n");

        return jsonB.toString();
    }

    @RequestMapping("/full")
   /* public String getFull(@RequestParam(value="date",defaultValue = "0")Integer date){
    if(date==0){
        date= DateFormatUtil.now();
    }

       Tuple3<String, String, String> fullOrderAmount = tradeStatesService.getFullOrderAmount(date);
       StringBuilder json = new StringBuilder();
       json.append("{\n" +
               "  \"status\": 0,\n" +
               "  \"data\": [\n" +
               "    {\n" +
               "      \"name\": \"访问人数\",\n" +
               "      \"value\":"+fullOrderAmount.f0+",\n" +
               "      \"color\":"+"\"green\"" +"},{   \"name\": \"周活跃数\",\n" +
               "      \"value\":"+fullOrderAmount.f1+",\n"+" \"color\":"+"\"blue\""+"   },\n" +
               "    {\n" +
               "      \"name\": \"月活跃数\",\n" +
               "      \"value\": "+fullOrderAmount.f2+",\n"+" \"color\": \"purple\"\n" +
               "    }\n" +
               "  ]\n" +
               "}");
return json.toString();
   }*/
    public String getFull(@RequestParam(value = "date", defaultValue = "0") Integer date) {
        if (date == 0) {
            date = DateFormatUtil.now();
        }
        TradeFullOrderAmount fullOrderAmount = tradeStatesService.getFullOrderAmount(date);

         String result=("{\n" +
                "  \"status\": 0,\n" +
                "  \"data\": [\n" +
                "    {\n" +
                "      \"name\": \"下单次数\",\n" +
                "      \"value\":"+fullOrderAmount.getOrderCount()+",\n" +
                "      \"color\":"+"\"#f05050\"" +"},{   \"name\": \"下单人数\",\n" +
                "      \"value\":"+fullOrderAmount.getUserCount()+",\n"+" \"color\":"+"\"blue\""+"   },\n" +
                "    {\n" +
                "      \"name\": \"下单金额\",\n" +
                "      \"value\": "+fullOrderAmount.getOrderAmount()+",\n"+" \"color\": \"purple\"\n" +
                "    }\n" +
                "  ]\n" +
                "}");
  return result;

    }

}


